﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.foreman
{
    public partial class foreman_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string keywords = string.Empty;//姓名 手机号
        protected int renzheng = 0;//身份认证 1 锤子认证 2非认证
        protected int level = 0;//等级认证 1星 2星 3星 4星 5星 
        protected int grade = 0;//工地评级 1 优秀 2良好 3一般
        protected string property = string.Empty;//是否置顶 推荐
        protected int status = -1;//0正常 1待验证 2待审核 3禁用

        protected void Page_Load(object sender, EventArgs e)
        {
            ChkAdminLevel("foreman_list", DTEnums.ActionEnum.View.ToString()); //检查权限
            this.keywords = DTRequest.GetQueryString("keywords");
            this.renzheng = DTRequest.GetQueryInt("renzheng");
            this.level = DTRequest.GetQueryInt("level");
            this.grade = DTRequest.GetQueryInt("grade");
            this.property = DTRequest.GetQueryString("property");
            this.status = DTRequest.GetQueryInt("status");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                TreeBind(); //绑定类别
                RptBind("a.id>0" + CombSqlTxt(this.keywords, this.renzheng, this.level, this.grade, this.property, this.status), "a.id desc");
            }
        }

        #region 绑定下拉框=================================
        private void TreeBind()
        {
            this.ddlRenZheng.Items.Clear();
            this.ddlRenZheng.Items.Add(new ListItem("所有认证", "0"));
            this.ddlRenZheng.Items.Add(new ListItem("锤子认证", "1"));
            this.ddlRenZheng.Items.Add(new ListItem("非认证", "2"));
            
            this.ddlLevel.Items.Clear();
            this.ddlLevel.Items.Add(new ListItem("所有等级", "0"));
            for (int i = 1; i < 6; i++)
            {
                this.ddlLevel.Items.Add(new ListItem(i + "星", i.ToString()));
            }

            this.ddlGrade.Items.Clear();
            this.ddlGrade.Items.Add(new ListItem("所有评级", ""));
            this.ddlGrade.Items.Add(new ListItem("优秀", "1"));
            this.ddlGrade.Items.Add(new ListItem("良好", "2"));
            this.ddlGrade.Items.Add(new ListItem("一般", "3"));
            
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (this.renzheng > 0)
            {
                this.ddlRenZheng.SelectedValue = this.renzheng.ToString();
            }
            if (this.level > 0)
            {
                this.ddlLevel.SelectedValue = this.level.ToString();
            }
            if (this.grade > 0)
            {
                this.ddlGrade.SelectedValue = this.grade.ToString();
            }
            this.ddlStatus.SelectedValue = this.status.ToString();
            this.ddlProperty.SelectedValue = this.property;
            this.txtKeywords.Text = this.keywords;
            BLL.foreman_user bll = new BLL.foreman_user();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("foreman_list.aspx", "renzheng={0}&level={1}&grade={2}&property={3}&keywords={4}&status={5}&page={6}",
                this.ddlRenZheng.SelectedValue, this.ddlLevel.SelectedValue, this.ddlGrade.SelectedValue, this.ddlProperty.SelectedValue, this.keywords, this.status.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords, int _renzheng, int _level, int _grade, string _property,int _status)
        {
            StringBuilder strTemp = new StringBuilder();

            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (user_name like '%" + _keywords + "%' or mobile like '%" + _keywords + "%' or email like '%" + _keywords + "%' or nick_name like '%" + _keywords + "%')");
            }

            if (_renzheng > 0)
            {
                strTemp.Append(" and fore_renzheng=" + _renzheng);
            }
            if (_grade > 0)
            {
                strTemp.Append(" and fore_grade=" + _grade);
            }
            if (_level > 0)
            {
                strTemp.Append(" and fore_level=" + _level);
            }
            if (!string.IsNullOrEmpty(_property))
            {
                switch (_property)
                {
                    case "isTop":
                        strTemp.Append(" and fore_istop=1");
                        break;
                    case "isRed":
                        strTemp.Append(" and fore_isred=1");
                        break;
                }
            }
            if(_status>-1)
                strTemp.Append(" and status="+_status);
           
            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("foreman_list_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        #region 返回状态=============================
        protected string GetUserStatus(int status)
        {
            string result = string.Empty;
            switch (status)
            {
                case 0:
                    result = "正常";
                    break;
                case 1:
                    result = "隐藏";
                    break;
                case 2:
                    result = "黑名单";
                    break;
                case 3:
                    result = "已删除";
                    break;
            }
            return result;
        }
        protected string GetGrade(int grade)
        {
            string result = string.Empty;
            switch (grade)
            {
                case 1:
                    result = "优秀";
                    break;
                case 2:
                    result = "良好";
                    break;
                case 3:
                    result = "一般";
                    break;
            }
            return result;
        }
        #endregion
        //设置操作
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ChkAdminLevel("foreman_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            int id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidId")).Value);
            BLL.foreman_user bll = new BLL.foreman_user();
            Model.foreman_user model = bll.GetModel(id);
            switch (e.CommandName)
            {
                case "lbtnIsTop":
                    if (model.fore_istop == 1)
                        bll.UpdateField(id, "fore_istop=0");
                    else
                        bll.UpdateField(id, "fore_istop=1");
                    break;
                case "lbtnIsRed":
                    if (model.fore_isred == 1)
                        bll.UpdateField(id, "fore_isred=0");
                    else
                        bll.UpdateField(id, "fore_isred=1");
                    break;
             
            }
            RptBind("a.id>0" + CombSqlTxt(this.keywords, this.renzheng, this.level, this.grade, this.property,this.status), "a.id desc");
            
        }

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("foreman_list.aspx", "renzheng={0}&level={1}&grade={2}&property={3}&keywords={4}&status={5}",
                this.ddlRenZheng.SelectedValue, this.ddlLevel.SelectedValue, this.ddlGrade.SelectedValue, this.ddlProperty.SelectedValue, this.keywords, this.status.ToString())
            );
        }

        //筛选
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("foreman_list.aspx", "renzheng={0}&level={1}&grade={2}&property={3}&keywords={4}&status={5}",
                this.ddlRenZheng.SelectedValue, this.ddlLevel.SelectedValue, this.ddlGrade.SelectedValue, this.ddlProperty.SelectedValue, this.keywords, this.ddlStatus.SelectedValue.ToString())
            );
        }
       
        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("foreman_list_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("foreman_list.aspx", "renzheng={0}&level={1}&grade={2}&property={3}&keywords={4}&status={5}",
                this.ddlRenZheng.SelectedValue, this.ddlLevel.SelectedValue, this.ddlGrade.SelectedValue, this.ddlProperty.SelectedValue, this.keywords, this.status.ToString())
            );
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("foreman_list", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.users bllUser = new BLL.users();
            BLL.foreman_user bllForeman = new BLL.foreman_user();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int user_id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidUserId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bllForeman.Delete(id) && bllUser.Delete(user_id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除工长" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
              Utils.CombUrlTxt("foreman_list.aspx", "renzheng={0}&level={1}&grade={2}&property={3}&keywords={4}&status={5}",
                this.ddlRenZheng.SelectedValue, this.ddlLevel.SelectedValue, this.ddlGrade.SelectedValue, this.ddlProperty.SelectedValue, this.keywords, this.status.ToString()));
        }
    }
}